import {cartAction} from '../funnames'
import { formatTime } from '@/utils/index'
import fly from '../../net/flyio'
import url from '../../net/serverurl'

const state = {
  cartList: [],
  errorPage: false,
  errorBtnText: '再试一次',
  total: 0,
  errorMsg: '',
  settleData:[],
  productsObj:[],
  settleSuccee:0,
  defaultCouponId: '',
  defaultshippingId:''
}

const mutations = {
  [cartAction.ACTION_CART_CLEAL](state) {
    state.page = 1
    state.hasMore = true
    state.movies = []
  },
  [cartAction.ACTION_SHOWEMPTY](state, {show, msg}) {
    state.errorPage = show
    state.errorMsg = msg
  },

  [cartAction.ACTION_CART_LIST](state, {data}) {
    if (data.successCode) {
      state.errorPage = false
      state.cartList = data.data.cart
      state.total = data.data.total
      if (state.total <= 0){
        state.errorPage = true
        state.errorMsg = '购物车空空如也'
        state.errorBtnText = '去别处逛逛'
      }
    } else {
      state.errorPage = true
      state.errorMsg = data.message
      state.errorBtnText = '再试一次'
    }
  },
  [cartAction.ACTION_CHECKEPRODUCTS](state,data){
    state.settleData = data
  },
  [cartAction.ACTION_SETTLE_CHECKE](state, { response }) {
    state.settleSuccee = response.successCode
    state.errorMsg = response.message
    if (response.successCode) {
      state.settleData = []
      state.settleData = response.data;

      if (state.settleData.default_coupon){
        state.defaultCouponId = state.settleData.default_coupon.coupon_id
      }

      if (state.defaultshippingId == ''){
        state.defaultshippingId = state.settleData.shipping_method[0];
      }

      state.settleData.coupons.forEach(function (item) {
        item.startTime = formatTime(new Date(item.start_time * 1000))
        item.endTime = formatTime(new Date(item.end_time * 1000))
      })

      state.settleData.shipping_method.forEach(function(item){
        item.shipping_description = item.shipping_description.replace(/\\n/g,'\n')
      })

      state.productsObj = []
      response.data.products.forEach(function (item) {
        let productItem = {
          product_id: item.product_id.toString(),
          product_sku_id: item.product_sku_id.toString(),
          product_qty: item.product_qty.toString(),
          cart_id: item.cart_id
        }
        state.productsObj.push(productItem)
      })
    }
  },
  [cartAction.ACTION_SHIPPINGID](state, data) {
    state.defaultshippingId = data
  },
}

const actions = {
  //购物车列表
  async getCarts({commit}) {
    state.settleSuccee = 0
    let data = await fly.post(url.URL_CART)
    commit(cartAction.ACTION_CART_LIST, {data})
  },
  //收银台
  async checkSettle({ state, commit }, {isSettlePage, checkedProduct, address_id, shipping_id, coupon_id}) {
    let data = {}
    if (!isSettlePage){
      data = {
        product_list: checkedProduct
      }
    }else{
      data = {
        product_list: state.productsObj,
        address_id: address_id,
        shipping_id: shipping_id,
        coupon_id: coupon_id
      }
    }
    let response = await fly.post(url.URL_CHECKOUT, data)
    commit(cartAction.ACTION_SETTLE_CHECKE, { response })

  }
}

export default {
  state,
  mutations,
  actions
}
